package service.state;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import object.StateContent;

import service.CommonServlet;

public class GetUserStates extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			int start = 0;
			int num = 1;
			int s_uNo=uNo;
			
			String uNoString = request.getParameter("uNo");
			if(uNoString!=null) Integer.parseInt(uNoString);
		
			String startString = request.getParameter("start");
			if(startString!=null) Integer.parseInt(startString);
			
			String numString = request.getParameter("num");
			if(numString!=null) Integer.parseInt(numString);
			
			String sql = "select * from state_mess where u_no = ? order by s_date desc limit ?, ?";			
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, s_uNo);
			pstmt.setInt(2, start);
			pstmt.setInt(3, num);
			ResultSet rs = pstmt.executeQuery();
			List<StateContent> states = new ArrayList<StateContent>();
			while(rs.next())
			{
				StateContent state = new StateContent();
				state.setsId(rs.getInt("s_id"));
				state.setsContent(rs.getString("s_content"));
				state.setsPic(rs.getString("p_data"));
				state.setsDate(rs.getString("s_date"));
				states.add(state);
			}
			sendObject(states, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			response.getWriter().println(e.getMessage());
		}
	}

	/**
	 * Constructor of the object.
	 */
	public GetUserStates() {
		super();
	}

}
